package com.panshi.controller;

import com.panshi.pojo.Hotnews;
import com.panshi.pojo.User;
import com.panshi.service.impl.HotnewsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@Controller
public class HotnewsController {

    @Autowired
    HotnewsServiceImpl hotnewsService;

    //添加新闻
    @GetMapping("/addnews")
    public String tonews() {
        return "/emp/addnews";
    }


    @Autowired
    Hotnews hotnews;

    @PostMapping("/addnews")
    public String addnews(HttpServletRequest req) {
        String cauthor = req.getParameter("cauthor");
        String uid = req.getParameter("uid");
        String titile = req.getParameter("titile");
        String ccontent = req.getParameter("ccontent");
        hotnews.setUid(Integer.valueOf(uid));
        hotnews.setCauthor(cauthor);
        hotnews.setTitile(titile);
        hotnews.setCcontent(ccontent);
        System.out.println("*********************** 增加+" + hotnews);
        hotnewsService.addNews(hotnews);
        return "redirect:/hotnews";
    }

    //    查询全部新闻
    @RequestMapping("/hotnews")
    public String hotnews(Model model) {
        Set<Hotnews> hotnews = hotnewsService.queryCommentsList();
        model.addAttribute("hotnews", hotnews);

        for (Hotnews h : hotnews
        ) {
            System.out.println("-----------------------------------");
            System.out.println(h);
        }
        return "/emp/Hotnews";
    }

    //    删除新闻
    @RequestMapping("/newsDel")
    public String newsDel(@RequestParam("cid") Integer cid) {
        hotnewsService.newsDel(cid);
        return "redirect:/hotnews";
    }

    //    修改
    @GetMapping("/newsUpdate")
    public String newsUpdate(@RequestParam("cid") Integer cid, Model model) {
        Hotnews newsByCid = hotnewsService.getNewsByCid(cid);
        model.addAttribute("updateNews", newsByCid);
        return "/emp/updatenews";
    }

    //修改提交
    @PostMapping("/newsUpdate")
    public String updatenews(HttpServletRequest req) {
        String cid = req.getParameter("cid");
        String cauthor = req.getParameter("cauthor");
        String uid = req.getParameter("uid");
        String titile = req.getParameter("titile");
        String ccontent = req.getParameter("ccontent");
        hotnews.setCid(Integer.valueOf(cid));
        hotnews.setUid(Integer.valueOf(uid));
        hotnews.setCauthor(cauthor);
        hotnews.setTitile(titile);
        hotnews.setCcontent(ccontent);
        System.out.println("*********************** 修改+" + hotnews);
        hotnewsService.updatenews(hotnews);
        return "redirect:/hotnews";
    }

    //    查看新闻
    @RequestMapping("/news")
    public String news(@RequestParam("cid") Integer cid, Model model) {
//        添加热度
        hotnewsService.addHot(cid);

        Set<Hotnews> hotnews = hotnewsService.queryCommentsList();
        for (Hotnews h : hotnews) {
            if (h.getCid() == cid) {
                System.out.println(h);
                model.addAttribute("news", h);
            }
        }
        return "/emp/news";
    }

}
